package com.gustuy.chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class TackSend implements Runnable{
    private DatagramSocket socket =null;
    private BufferedReader reader=null;
    private int toport;
    private int myport;
    private String toIP;

    public TackSend(int toport, int myport, String toIP) {
        this.toport = toport;
        this.myport = myport;
        this.toIP = toIP;
        try {
            socket=new DatagramSocket();
            reader=new BufferedReader(new InputStreamReader(System.in));
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                String data = reader.readLine();
                byte[] datas = new byte[1024];
                datas = data.getBytes();
                DatagramPacket packet = new DatagramPacket(datas, 0, datas.length,
                        InetAddress.getByName(toIP), toport);
                socket.send(packet);
                if(data.equals("bye")){
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
